﻿using System.Collections.Generic;
using System.Xml.Linq;
using Engine;
using TemplatesDatabase;
using XmlUtilities;

namespace Game
{
    public static class ModItemManger
    {
        public static List<ItemData> m_itemDatas = new List<ItemData>();

        public static Color MainUiColor = new Color(255, 0, 0);

        public static bool CreaterMode;

        public static int MaxSellCount;

        public static void Initialize(ValuesDictionary valuesDictionary)
        {
            CreaterMode = valuesDictionary.GetValue("CreaterMode", defaultValue: false);
            MainUiColor = valuesDictionary.GetValue("MainUiColor", Color.Red);
            MaxSellCount = valuesDictionary.GetValue("MaxSellCount", 40);
            foreach (XElement item in ContentManager.Get<XElement>("ItemDatas").Elements())
            {
                m_itemDatas.Add(new ItemData
                {
                    Index = ((XmlUtils.GetAttributeValue<string>(item, "DisplayName") == "Null") ? (-1) : CraftingRecipesManager.DecodeResult(XmlUtils.GetAttributeValue<string>(item, "DisplayName"))),
                    BuyMoney = XmlUtils.GetAttributeValue<int>(item, "BuyMoney"),
                    HitTime = XmlUtils.GetAttributeValue<double>(item, "HitTime"),
                    AimTime = XmlUtils.GetAttributeValue<float>(item, "AimTime"),
                    HitDistance = XmlUtils.GetAttributeValue<float>(item, "HitDistance"),
                    MoveScreenWhenAim = XmlUtils.GetAttributeValue<bool>(item, "MoveScreenWhenAim"),
                    SellMoney = XmlUtils.GetAttributeValue<int>(item, "SellMoney"),
                    Quality = XmlUtils.GetAttributeValue<float>(item, "Quality")
                });
            }
        }

        public static ItemData GetItemData(int index)
        {
            foreach (ItemData itemData in m_itemDatas)
            {
                if (itemData.Index == index)
                {
                    return itemData;
                }
            }

            return m_itemDatas[0];
        }

        public static string GetName(string name)
        {
            foreach (XElement item in ContentManager.Get<XElement>("GameDatas").Descendants("LanguageData"))
            {
                if (XmlUtils.GetAttributeValue<string>(item, "Name") == name)
                {
                    return XmlUtils.GetAttributeValue<string>(item, "ChineseText");
                }
            }

            return string.Empty;
        }

        public static void Load(ValuesDictionary valuesDictionary)
        {
        }

        public static void Save(ValuesDictionary valuesDictionary)
        {
            valuesDictionary.SetValue("CreaterMode", CreaterMode);
            valuesDictionary.SetValue("MainUiColor", MainUiColor);
            valuesDictionary.SetValue("MaxSellCount", MaxSellCount);
        }
    }
}